package com.example.crm01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Business;
import com.example.crm01.mapper.BusinessMapper;
import com.example.crm01.service.BusinessService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.business.BusinessAddForm;
import com.example.crm01.service.form.business.BusinessListForm;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    private final BusinessMapper businessMapper;

    public BusinessServiceImpl(BusinessMapper businessMapper) {
        this.businessMapper = businessMapper;
    }

    @Override
    public Page<Business> businessList(BusinessListForm form) {
        Page<Business> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getClientName())) {
            queryWrapper.like(Business::getClientName, form.getClientName());
        }
        if (StringUtils.isNotEmpty(form.getBusinessName())) {
            queryWrapper.like(Business::getBusinessName, form.getBusinessName());
        }
        if (ObjectUtils.isNotEmpty(form.getBusinessStatus())) {
            queryWrapper.eq(Business::getBusinessStatus, form.getBusinessStatus());
        }

        if (StringUtils.isNotEmpty(form.getTicketNumber())) {
            queryWrapper.eq(Business::getTicketNumber, form.getTicketNumber());
        }

        if (ObjectUtils.isNotEmpty(form.getTicketType())) {
            queryWrapper.eq(Business::getTicketType, form.getTicketType());
        }

        if (ObjectUtils.isNotEmpty(form.getBusinessNumber())) {
            queryWrapper.eq(Business::getBusinessNumber, form.getBusinessNumber());
        }

        if (ObjectUtils.isNotEmpty(form.getTicketStatus())) {
            queryWrapper.eq(Business::getTicketStatus, form.getTicketStatus());
        }

        queryWrapper.ne(Business::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean insertBusiness(BusinessAddForm form) {
        //todo 根据类型分类新增
        Business business = new Business();
        business.setClientId(form.getClientId());
        business.setClientName(form.getClientName());
        business.setBusinessName(form.getBusinessName());
        business.setBusinessStatus(form.getBusinessStatus());
        business.setInstallationAddress(form.getInstallationAddress());
        business.setTicketNumber(form.getTicketNumber());
        business.setTicketType(form.getTicketType());
        business.setTicketConsentDate(form.getTicketConsentDate());
        business.setTicketReturnDate(form.getTicketReturnDate());
        business.setBusinessNumber(form.getBusinessNumber());

        //-----
        business.setTicketStatus(0);
        business.setCreateBy("1");
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateBy("1");
        business.setUpdateTime(LocalDateTime.now());
        business.setDel_flag(0);
        int insert = businessMapper.insert(business);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateBusiness(BusinessAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())) {
            throw new RuntimeException("修改业务信息，主键id不能为空");
        }
        Business business = baseMapper.selectById(form.getId());
        business.setClientId(form.getClientId());
        business.setClientName(form.getClientName());
        business.setBusinessName(form.getBusinessName());
        business.setBusinessStatus(form.getBusinessStatus());
        business.setInstallationAddress(form.getInstallationAddress());
        business.setTicketNumber(form.getTicketNumber());
        business.setTicketType(form.getTicketType());
        business.setTicketConsentDate(form.getTicketConsentDate());
        business.setTicketReturnDate(form.getTicketReturnDate());
        business.setBusinessNumber(form.getBusinessNumber());

        //-----
        business.setUpdateBy("1");
        business.setUpdateTime(LocalDateTime.now());
        business.setDel_flag(1);
        int update = baseMapper.updateById(business);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteBusiness(BaseIdForm form) {
        Business business = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(business)) {
            throw new RuntimeException("业务信息不存在删除失败");
        }
        business.setUpdateBy("1");
        business.setUpdateTime(LocalDateTime.now());
        business.setDel_flag(2);
        int update = baseMapper.updateById(business);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Business detailsBusiness(BaseIdForm form) {
        Business business = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(business)) {
            throw new RuntimeException("业务信息不存在，获取详情信息失败");
        }
        return business;
    }
}
